package com.common.baselibrary.sharepreference;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;


import com.common.baselibrary.ApplicationManager;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Function:  <br/>
 * date: ${date} ${time} <br/>
 *
 * @author ${user}
 * @version ${enclosing_type}${tags}
 */
public class PrefWrapper {


    private static final Byte[] mLock = new Byte[0]; // 字节数组，便于序列化，并减少指令个数（相关讨论参见：http://stackoverflow.com/questions/2120437/object-vs-byte0-as-lock）

    private static LinkedHashMap<String, PrefWrapper> preWrappers = new LinkedHashMap<String, PrefWrapper>();

    private SharedPreferences mPreferences;

    private SharedPreferences.Editor mEditor;

    /**
     * 私有构造函数
     *
     * @param context
     * @param preferenceName
     */
    private PrefWrapper(final Context context, String preferenceName) {
        mPreferences = context.getSharedPreferences(preferenceName,
                Activity.MODE_PRIVATE);
        mEditor = mPreferences.edit();
    }

    public static PrefWrapper defaultWrapper() {
        return instance(ApplicationManager.getApplicationContext(), "pars_sharepreference");
    }


    /**
     * double check单例模式（线程安全）
     *
     * @param context
     * @param preferenceName
     * @return
     */
    public static PrefWrapper instance(final Context context,
                                       String preferenceName) {
        if (context == null) {
            return null;
        }
        PrefWrapper wrapper = preWrappers.get(preferenceName);
        if (wrapper != null) {
            return wrapper;
        }

        synchronized (mLock) {
            if (null == wrapper) {
                wrapper = new PrefWrapper(context, preferenceName);
                preWrappers.put(preferenceName, wrapper);
            }
        }

        return wrapper;
    }

    public Map<String, ?> getAll() {
        return mPreferences.getAll();
    }

    public boolean contains(String key) {
        return mPreferences.contains(key);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return mPreferences.getBoolean(key, defValue);
    }

    public float getFloat(String key, float defValue) {
        return mPreferences.getFloat(key, defValue);
    }

    public int getInt(String key, int defValue) {
        return mPreferences.getInt(key, defValue);
    }

    public long getLong(String key, long defValue) {
        return mPreferences.getLong(key, defValue);
    }

    public String getString(String key, String defValue) {
        return mPreferences.getString(key, defValue);
    }

    public void putBoolean(String key, boolean b) {
        mEditor.putBoolean(key, b);
        mEditor.apply();
        return;
    }

    public void putInt(String key, int i) {
        mEditor.putInt(key, i);
        mEditor.apply();
        return;
    }

    public void putFloat(String key, float f) {
        mEditor.putFloat(key, f);
        mEditor.apply();
        return;
    }

    public void putLong(String key, long l) {
        mEditor.putLong(key, l);
        mEditor.apply();
        return;
    }

    public void putString(String key, String s) {
        mEditor.putString(key, s);
        mEditor.apply();
        return;
    }

    public void removeKey(String key) {
        mEditor.remove(key);
        mEditor.apply();
        return;
    }

    public void removeKeyList(List<String> keyList) {
        if (keyList == null || keyList.size() == 0) {
            return;
        }
        for (String key : keyList) {
            mEditor.remove(key);
        }
        mEditor.commit();
    }

    public void clear() {
        mEditor.clear();
    }

    /**
     * 注册监听器
     *
     * @param listener
     */
    public void registerOnSharedPreferenceChangeListener(
            SharedPreferences.OnSharedPreferenceChangeListener listener) {
        mPreferences.registerOnSharedPreferenceChangeListener(listener);
    }

    /**
     * 取消注册监听器
     *
     * @param listener
     */
    public void unregisterOnSharedPreferenceChangeListener(
            SharedPreferences.OnSharedPreferenceChangeListener listener) {
        mPreferences.unregisterOnSharedPreferenceChangeListener(listener);
    }
}
